using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using iTron.Model;
using iTron.Model.GridItems;

namespace iTron.View
{
    class TrailView
    {
        public static int SPRITE_HEIGHT = 29, SPRITE_WIDTH = 29;
        private ContentManager cm;
        public Texture2D texture { get; set; }

        public TrailView(ContentManager cm, string sprite)
        {
            this.cm = cm;
            texture = cm.Load<Texture2D>(sprite);
        }

        public Vector2 getPosition(Bike bike, Vector2 position)
        {
            Vector2 newPosition = new Vector2();
            double x = 0, y = 0;

            x = bike.x * 4;
            y = bike.y * 4;

            newPosition.X = position.X;
            newPosition.Y = position.Y;

            if (bike.directionFrom == Utils.DIRECTION_TOP)
            {
                if (bike.directionTo == Utils.DIRECTION_TOP)
                {
                    newPosition.X = position.X + (float) y;
                    newPosition.Y = position.Y;
                }
            } else if (bike.directionFrom == Utils.DIRECTION_RIGHT) {
                if (bike.directionTo == Utils.DIRECTION_RIGHT)
                {
                    // TODO - GI:++
                    newPosition.X = position.X;
                    newPosition.Y = position.Y + (SPRITE_WIDTH - (float)x);
                }
            } else if (bike.directionFrom == Utils.DIRECTION_BOTTOM) {
                if (bike.directionTo == Utils.DIRECTION_BOTTOM)
                {
                    // TODO - GI:++
                    newPosition.X = position.X - (SPRITE_HEIGHT - (float)y);
                    newPosition.Y = position.Y;
                }
            } else if (bike.directionFrom == Utils.DIRECTION_LEFT) {
                if (bike.directionTo == Utils.DIRECTION_LEFT)
                {
                    newPosition.X = position.X;
                    newPosition.Y = position.Y - (float)x;
                }
            }

            return newPosition;
        }

        public Rectangle getRectangle()
        {
            return new Rectangle(0 * SPRITE_WIDTH, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
        }

        /**
         * If painting bike item
         */
        public Rectangle getRectangle(Bike bike)
        {
            if (bike.directionFrom == Utils.DIRECTION_TOP)
            {
                if (bike.directionTo == Utils.DIRECTION_TOP)
                {
                    return new Rectangle(1 * SPRITE_WIDTH, 0, SPRITE_WIDTH, SPRITE_HEIGHT - (bike.y * 4));
                }
            } else if (bike.directionFrom == Utils.DIRECTION_RIGHT) {
                if (bike.directionTo == Utils.DIRECTION_RIGHT)
                {
                    // TODO
                    return new Rectangle(4 * SPRITE_WIDTH, 0, bike.x * 4, SPRITE_HEIGHT);
                }
            } else if (bike.directionFrom == Utils.DIRECTION_BOTTOM) {
                if (bike.directionTo == Utils.DIRECTION_BOTTOM)
                {
                    // TODO
                    return new Rectangle(1 * SPRITE_WIDTH, 0, SPRITE_WIDTH, bike.y * 4);
                }
            } else if (bike.directionFrom == Utils.DIRECTION_LEFT) {
                if (bike.directionTo == Utils.DIRECTION_LEFT)
                {
                    return new Rectangle(4 * SPRITE_WIDTH, 0, SPRITE_WIDTH - (bike.x * 4), SPRITE_HEIGHT);
                }
            }

            // Should never be called
            return getRectangle(bike, true);
        }

        public Rectangle getRectangle(GridItem gridItem, bool isBike)
        {
            int directionFrom = gridItem.directionFrom, directionTo = gridItem.directionTo;

            if (directionFrom == directionTo)
            {
                if (directionFrom == Utils.DIRECTION_TOP
                    || directionFrom == Utils.DIRECTION_BOTTOM)
                {
                    return new Rectangle(1 * SPRITE_WIDTH, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
                }
                else
                {
                    return new Rectangle(4 * SPRITE_WIDTH, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
                }
            }
            else
            {
                if ((directionFrom == Utils.DIRECTION_TOP && directionTo == Utils.DIRECTION_LEFT) ||
                    (directionFrom == Utils.DIRECTION_RIGHT && directionTo == Utils.DIRECTION_BOTTOM))
                {
                    return new Rectangle(0 * SPRITE_WIDTH, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
                }
                else if ((directionFrom == Utils.DIRECTION_BOTTOM && directionTo == Utils.DIRECTION_RIGHT) ||
                    (directionFrom == Utils.DIRECTION_LEFT && directionTo == Utils.DIRECTION_TOP))
                {
                    return new Rectangle(2 * SPRITE_WIDTH, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
                }
                else if ((directionFrom == Utils.DIRECTION_TOP && directionTo == Utils.DIRECTION_RIGHT) ||
                    (directionFrom == Utils.DIRECTION_LEFT && directionTo == Utils.DIRECTION_BOTTOM))
                {
                    return new Rectangle(3 * SPRITE_WIDTH, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
                }
                else if ((directionFrom == Utils.DIRECTION_BOTTOM && directionTo == Utils.DIRECTION_LEFT) ||
                    (directionFrom == Utils.DIRECTION_RIGHT && directionTo == Utils.DIRECTION_TOP))
                {
                    return new Rectangle(5 * SPRITE_WIDTH, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
                }
            }

            return new Rectangle();
        }
    }
}
